﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace La.Code.Xml
{
    /// <summary>
    /// xml 序列化 反序列化
    /// </summary>
    public static class XmlSerializerHelper
    {
        #region 基础 序列化 反序列化

        /// <summary>
        /// 将一个对象 用xml序列化成字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns>序列化结果的字符串</returns>
        public static string XMLSerialize<T>(T entity)
        {
            try
            {
                //字符流
                StringBuilder buffer = new StringBuilder();
                //序列化这个对象
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                using (TextWriter writer = new StringWriter(buffer))
                {
                    serializer.Serialize(writer, entity);
                }
                return buffer.ToString();
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="xmlString">XML字符串</param>
        /// <returns>实体对象</returns>
        public static T DeXMLSerialize<T>(string xmlString)
        {
            T cloneObject = default(T);
            try
            {

                StringBuilder buffer = new StringBuilder();
                buffer.Append(xmlString);
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                using (TextReader reader = new StringReader(buffer.ToString()))
                {
                    Object obj = serializer.Deserialize(reader);
                    cloneObject = (T)obj;//强制转换为需要的对象类型
                }
                return cloneObject;
            }
            catch
            {
                return cloneObject;
            }
        }
        #endregion

 
        /// <summary>
        /// 根据XML文件路径 反序列对象（默认编码集Encoding.UTF8）
        /// </summary>
        /// <typeparam name="T">反序列化 对象</typeparam>
        /// <param name="xmlFilePath">XML文件路径 </param>
        /// <returns>反序列化 对象</returns>
        public static T XmlReadFilePath<T>(string FilePath)
        {
            T cloneObject = default(T);
            try
            {
                FileHelperFrom fileHelper = new FileHelperFrom();
                cloneObject = DeXMLSerialize<T>(fileHelper.ReadFile(FilePath, Encoding.UTF8)); 
                return cloneObject;
            }
            catch
            {
                return cloneObject;
            }
        }


        /// <summary>
        /// 根据文件路径和字符集编码 反序列化 
        /// </summary>
        /// <typeparam name="T"> 反序列化 对象</typeparam>
        /// <param name="xmlFilePath">XML文件路径（全路径）</param>
        /// <param name="encoding">字符集编码</param>
        /// <returns>反序列化 对象</returns>
        public static bool XmlWriteFilePath<T>(string xmlFilePath, T entity)
        {
            string tempStr = XMLSerialize(entity);
            try
            {
                FileHelperFrom fileHelper = new FileHelperFrom();
                return fileHelper.WriteFile(xmlFilePath, tempStr, false);
                  
            }
            catch
            {
                return false;
            }
        }


    }

}
